package com.capgemini.business.controller;

import com.capgemini.business.entity.Files;
import com.capgemini.business.mapper.FilesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/files")
public class FilesController {

    @Autowired
    private FilesMapper filesMapper;

    // 调用获取所有人员数据的接口
    @GetMapping("/{id}")  // id查询
    public Object findById(@PathVariable Long id) {
        Files file = filesMapper.findById(id);
        if (file != null) {
            return file;
        } else {
            return "用户未找到";
        }
    }

    @GetMapping("/")  //
    public Object index(@RequestParam(required = false) Long id) {
        if (id == null) {
            return filesMapper.findAll();
        } else {
            return findById(id);
        }
    }

    // 调用添加人员数据的接口
    @PostMapping("/add")
    public String addFile(@RequestBody Files file) {
        try {
            filesMapper.insertFiles(file);
            return "文件添加成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "文件添加失败，请重试！";
        }
    }

    // 调用修改项目数据的接口
    @PutMapping("/update")
    public String updateFile(@RequestBody Files file) {
        try {
            filesMapper.updateFiles(file);
            return "文件更新成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "文件更新失败，请重试！";
        }
    }

    // 调用删除人员数据的接口 通过id
    @DeleteMapping("/delete/{id}")
    public String deleteFile(@PathVariable Long id) {
        try {
            filesMapper.deleteFiles(id);
            return "文件删除成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "文件删除失败，请重试！";
        }
    }
}
